package info.xiao.algorithm.sort;

import java.util.Arrays;

/**
 * @Classname ChooseSort 选择排序
 * @Description TODO
 * @Date 2022/10/11 15:26
 * @Created by 晓龙Oba
 */
public class ChooseSort {
    public static void main(String[] args) {
        int[] totalMarkArray = {280, 231, 236, 150, 297};
        totalMarkArray = sort(totalMarkArray);
        Arrays.stream(totalMarkArray).forEach(item -> System.out.println(item));
    }

    private static int[] sort(int[] totalMarkArray) {
        for (int i = 0; i < totalMarkArray.length - 1; i++) {
            // 记录最大值索引位置(坐标)
            Integer index = i;
            // 比较
            for (int j = i + 1; j < totalMarkArray.length; j++) {
                if (totalMarkArray[index] < totalMarkArray[j]) {
                    index = j;
                }
            }
            // 判断是否需要交换
            if (index != i) {
                totalMarkArray[index] += totalMarkArray[i];
                totalMarkArray[i] = totalMarkArray[index] - totalMarkArray[i];
                totalMarkArray[index] -= totalMarkArray[i];
            }
        }
        return totalMarkArray;
    }

}
